package test.movies;

import static org.openqa.selenium.support.PageFactory.initElements;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

public class MovieInfo extends PageMovies {
		

	    // Logger variable
	    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(MovieInfo.class);
	  
	    //Element on page with Movie information and actions for movie
	     private String information ="//div[@class='maininfo_full']";
	     
	     // Button Remove on Movie Info Page 
	     private String buttonRemove =  "//img[@title='Remove']";
	     
	     // Button LogOut on Main page
	     private String buttonLogout =  "//a[contains(text(),'Log out')]";

	    
	    /**
	     * Constructor of the MovieInfo class
	     * @param driver - configured driver
	     */
	    public MovieInfo(WebDriver driver) {
	        super(driver);
	        initElements(driver, this);
	    }


	    /**
	     * Check if it is the right page
	     * @return true if right page
	     */
	    public boolean checkIfMovieInfoIsOpen() {

	        LOGGER.info("Checking whether the user on the Movie Info Page");
	        try{

	            WebElement checkPage = new WebDriverWait(driver,20).until(visibilityOfElementLocated(By.xpath(information)));
	            LOGGER.info("MovieInfo Page is open");

	            return true;
	        }
	        catch (NullPointerException e){
	            LOGGER.error("Movie Info Page is disable!!!");
	            return false;}

	        catch (TimeoutException e){

	            LOGGER.error("Movie Info Page is not open!!!");
	            return false;
	        }

	    }


	    /**
	     * this method remove film description
	     * @return true
	     */
	    public MainPage removeDescription() {

	        try{

	            WebElement movie = new WebDriverWait(driver,20).until(elementToBeClickable(By.xpath(buttonRemove)));
	            movie.click();
	            LOGGER.info("Remove Description");
	            confirmAction();

	            return new MainPage(driver);
	        }
	        catch (NullPointerException e){
	            LOGGER.error("Movie Info Page is disable!!!");
	            return null;}

	        catch (TimeoutException e){

	            LOGGER.error("Button Remove is not found!!!");
	            return null;
	        }

	    }

	    // method switches to Confirmation PopUp and click Ok
	    public boolean confirmAction(){
	        try {
	            WebDriverWait wait = new WebDriverWait(driver, 20);
	            wait.until(alertIsPresent()).accept();
	            LOGGER.info("Confirm Action");
	            return true;
	        } catch (NoAlertPresentException e) {
	            return false;
	        }
	        catch (TimeoutException e) {
	            return false;
	        }
	    }

	    /**
	     * this method check data of film description
	     * @return true
	     */
	    public boolean checkMovieInfo(String name, String year) {

	        boolean check = true;
	        try{

	            WebElement movie = new WebDriverWait(driver,20).until(elementToBeClickable(By.xpath(information)));
	            String text = movie.getText();
	            LOGGER.info("Movie Info is " + text);

	            if (text.contains(name)){
	                LOGGER.info("Movie Name is correct");
	                }
	            else {LOGGER.info("Movie Info is wrong!!!");
	            check = false;}

	            if (text.contains(year)){
	                LOGGER.info("Movie Year is correct");
	            }
	            else {LOGGER.info("Movie Year is wrong!!!!");
	                check = false;}

	            if (check == true)  {
	                LOGGER.info("Movie Info is correct");
	                return true;
	            }
	           else {
	                LOGGER.info("Movie Info is wrong!!!");
	                return false;
	            }

	        }
	        catch (NullPointerException e){
	            LOGGER.error("Movie Info Page is disable!!!");
	            return false;}

	        catch (TimeoutException e){

	            LOGGER.error("Movie Info is not found!!!");
	            return false;
	        }

	    }
}
